package com.eagle.common.core.support;

import com.github.pagehelper.PageInfo;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;
import java.util.List;

/**
 * 接口统一返回类型
 *
 * @author wcy
 * @date Created in 2021/8/28 18:25 <br>
 */
@Getter
@Setter
public class ResultMessage<T> implements Serializable {

    private static final String SUCCESS_MESSAGE = "success";

    private String code;
    /**
     * 成功为success，其他为失败原因
     */
    private Object message = SUCCESS_MESSAGE;
    /**
     * 数据结果集
     */
    private T data;
    /**
     * 分页总条数
     */
    private long total;
    /**
     * 当前页数
     */
    private int current;

    private int size;

    public ResultMessage() {
    }

    public ResultMessage(T data) {
        this.data = data;
    }

    public ResultMessage(Object message, T data) {
        this.message = message;
        this.data = data;
    }

    public ResultMessage(String code, Object message, T data) {
        this.message = message;
        this.data = data;
        this.code = code;
    }

    public static <T> ResultMessage<T> success(T data) {
        if (data instanceof EsPage) {
            EsPage esPage = (EsPage) data;
            return ResultMessage.success(esPage);
        }
        return builder().data(data).build();
    }

    public static <T> ResultMessage<List<T>> success(PageInfo<T> page) {
        return builder()
                .current(page.getPageNum())
                .total(page.getTotal())
                .size(page.getSize())
                .data(page.getList()).build();
    }

    public static <T> ResultMessage<List<T>> success(EsPage<T> page) {
        return Builder.builder()
                .current(page.getCurrent())
                .total(page.getTotal())
                .size(page.getSize())
                .data(page).build();
    }

    public static <T> ResultMessage<T> success(String message, T data) {
        return builder().message(message).data(data).build();
    }

    public static ResultMessage error(String code, String message) {
        return builder().code(code).message(message).build();
    }

    public static ResultMessage defaultError(String message) {
        return builder().code("500").message(message).build();
    }

    public static Builder builder() {
        return new Builder();
    }

    public static final class Builder<T> {
        private String code;
        /**
         * 成功为success，其他为失败原因
         */
        private Object message = SUCCESS_MESSAGE;
        /**
         * 数据结果集
         */
        private T data;

        private long total;

        private int current;

        private int size;

        private Builder() {
        }

        public static Builder builder() {
            return new Builder();
        }

        public Builder code(String code) {
            this.code = code;
            return this;
        }

        public Builder message(Object message) {
            this.message = message;
            return this;
        }


        public Builder data(T data) {
            this.data = data;
            return this;
        }

        public Builder current(int current) {
            this.current = current;
            return this;
        }

        public Builder size(int size) {
            this.size = size;
            return this;
        }

        public Builder total(long total) {
            this.total = total;
            return this;
        }

        public ResultMessage build() {
            ResultMessage<T> resultMessage = new ResultMessage<>();
            resultMessage.setMessage(message);
            resultMessage.setData(data);
            resultMessage.setCurrent(current);
            resultMessage.setSize(size);
            resultMessage.setTotal(total);
            resultMessage.setCode(code);
            return resultMessage;
        }
    }
}